#!/usr/bin/env python3
import pika
import time
import json
from subprocess import Popen, PIPE
import sqlite3
import sys


def sendImg2VecMsg(channel):
    taskid = "2321"
    option = "addface" # addface,delface,delperson
    url = "http://0.0.0.0:8000/Sheldon_3.jpg"  
    faceid = "Sheldon"

    dict = {}
    dict.update({'taskid':taskid})
    dict.update({'option':option})
    dict.update({'url':url})
    dict.update({'faceid':faceid})
    
    commandmsg = json.dumps(dict)

    # 发布消息
    channel.basic_publish(exchange='PDMoutTopic',
                          routing_key=routing_key,
                          body=commandmsg)
    print("msg: ", commandmsg)
    print(" [x] Sent msg out!")


if __name__ == '__main__':
    '''
    发布img imgpath指令至rabbitMQ服务器
    '''
    # 建立连接
    credentials = pika.PlainCredentials('aiot', 'aiot')
    Parameter = pika.ConnectionParameters('127.0.0.1', 5672, '/', credentials)
    connection = pika.BlockingConnection(Parameter)
    channel = connection.channel()

    # 声明/指定消息队列
    #channel.queue_declare(queue='PDMoutQueue')
    channel.exchange_declare(exchange='PDMoutTopic', exchange_type='topic')
    routing_key = sys.argv[1] if len(sys.argv) > 1 else '111.111.111'

    # 生成img to face feature vector调用指令消息内容,发布消息
    sendImg2VecMsg(channel)

    # 永久持续运行
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

